package com.prd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.common.baseMapper.ApiResult;
import com.prd.model.Attendance;
import com.prd.mapper.AttendanceMapper;
import com.prd.service.AttendanceService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;

/**
 * 出勤打卡数据表服务实现类
 *
 * @author wuzhengchao
 * @since 2023-10-18 23:51:04
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {
    @Resource
    private AttendanceMapper attendanceMapper;
    protected Logger logger = LoggerFactory.getLogger(AttendanceServiceImpl.class);

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ApiResult queryById(Serializable id) {
        try {
            Attendance attendance = this.attendanceMapper.selectById(id);
            return ApiResult.success(attendance);
        } catch (Exception e) {
            logger.info("通过ID查询单条数据错误日志：", e);
            return ApiResult.error("通过ID查询单条数据失败");
        }
    }

    /**
     * 分页查询
     *
     * @param attendance 筛选条件
     * @return 查询结果
     */
    @Override
    public ApiResult queryByPage(Attendance attendance) {
        try {

            return ApiResult.success("分页查询");
        } catch (Exception e) {
            logger.info("分页查询失败：", e);
            return ApiResult.error("分页查询失败");
        }
    }

    /**
     * 新增数据
     *
     * @param attendance 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public ApiResult insert(Attendance attendance) {
        try {
            QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
            //TODO 新增的业务操作
            List<Attendance> attendanceList = this.attendanceMapper.selectList(wrapper);
            if (!CollectionUtils.isEmpty(attendanceList)) {
                return ApiResult.error("名称重复");
            }
            //TODO 新增的业务操作

            int insert = this.attendanceMapper.insert(attendance);
            if (insert == 0) {
                return ApiResult.error("保存失败");
            }
            return ApiResult.success("保存成功");

        } catch (Exception e) {
            logger.info("保存失败：", e);
            throw new RuntimeException("保存失败：" + e.getMessage());
        }
    }

    /**
     * 修改数据
     *
     * @param attendance 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public ApiResult update(Attendance attendance) {
        try {
            this.attendanceMapper.updateById(attendance);
            return ApiResult.success("修改成功");
        } catch (Exception e) {
            logger.info("修改失败：", e);
            throw new RuntimeException("修改失败：" + e.getMessage());
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public ApiResult delete(Serializable id) {
        try {
            Attendance attendance = this.attendanceMapper.selectById(id);
            int update = this.attendanceMapper.updateById(attendance);
            if (update < 1) {
                return ApiResult.error("删除失败");
            }
        } catch (Exception e) {
            logger.info("错误日志：", e);
            return ApiResult.error("删除失败");
        }
        return ApiResult.error("删除成功");
    }

    @Override
    @Transactional
    public ApiResult deleteBatch(List<Long> ids) {
        try {
            List<Attendance> attendanceList = attendanceMapper.selectBatchIds(ids);
            for (Attendance attendance : attendanceList) {
                //TODO 业务操作
            }
            boolean batch = this.updateBatchById(attendanceList);
            if (!batch) {
                return ApiResult.error("批量删除失败");
            }
            return ApiResult.success("批量删除成功");
        } catch (Exception e) {
            logger.info("批量删除失败：", e);
            throw new RuntimeException("批量删除失败：" + e.getMessage());
        }
    }
}
